package com.danskin.wechat.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.danskin.wechat.entity.StoreInfo;
import com.danskin.wechat.entity.SysArea;
import com.danskin.wechat.model.AccessResponse;
import com.danskin.wechat.model.LoginUser;
import com.danskin.wechat.service.StoreInfoService;
import com.danskin.wechat.service.SysAreaService;
import com.danskin.wechat.util.JsonUtils;
import com.danskin.wechat.util.LogUtil;
import com.danskin.wechat.util.Page;

@Controller
@RequestMapping("store")
public class StoreController extends BaseController {

	@Autowired
	private StoreInfoService storeService;

	@Autowired
	private SysAreaService areaService;
	
	private List<StoreInfo> list=null;

	@RequestMapping(value = "/listStore", produces = "application/json;charset=UTF-8")
	public String listStore() {

		return "store/store_list";
	}
	@RequestMapping(value = "/listStoreQR", produces = "application/json;charset=UTF-8")
	public String listStoreQR() {
		
		return "store/store_list_show";
	}
	
	@RequestMapping(value = "/getAllArea", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Object getAllArea() {
		List<SysArea> list2 = areaService.getSysAllAreaList();
		return list;
	}
	 

	/**
	 * 展示店铺列表
	 * 
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/showStoreList", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Object showAccountList(Page page) throws Exception {
		Map map = getRequestParameter(getRequest());
		LoginUser loginUser = getLoginUser();
		map.put("mpId", loginUser.getMpId());
		page.setMap(map);

		list = storeService.listPageStrore(page);
		for (StoreInfo storeInfo : list) {
			String p = areaService.getSysAreaByCode(storeInfo.getProvince());
			storeInfo.setProvince(p);
			String c = areaService.getSysAreaByCode(storeInfo.getCity());
			storeInfo.setCity(c);
			String d = areaService.getSysAreaByCode(storeInfo.getDistrict());
			storeInfo.setDistrict(d);
		}
		
		return pagelistToJSONForBootStrapTable(list, page);
	}

	@RequestMapping(value = "/updateStore", produces = "application/json;charset=UTF-8")
	public String updateStore(Integer id, Model model) throws Exception {
		if (id!=null) {
			if(list.size()>0){
				StoreInfo storeInfo = storeService.selectByPrimaryKey(id);
				
				model.addAttribute("map", storeInfo);
		}
	}
		return "store/store_ maintenance";
	}
	@RequestMapping(value = "/toUpdateStoreInfo", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String toUpdateStoreInfo(@RequestBody JSONObject json) throws Exception {
		AccessResponse<StoreInfo> access = new AccessResponse<StoreInfo>();
		if (null!=json) {
			StoreInfo storeInfo = JsonUtils.jsonToPojo(json.toString(), StoreInfo.class);

			int i = storeService.updateByPrimaryKeySelective(storeInfo);
			if (i > 0) {
				access.setErrorCode("0");
				access.setErrorMsg("更新成功");
			} else {
				access.setErrorCode("1");
				access.setErrorMsg("更新异常");
			}
		} else {
			access.setErrorCode("1");
			access.setErrorMsg("接收数据为空");
		}

		return JsonUtils.objectToJson(access);
	}
	
	/**
	 * 更新店铺vip状态
	 */
	

	/**
	 * 根据门店ID生成门店二维码
	 * 
	 * @param storeInfoId
	 * @return
	 * @author makunsmac
	 */
	@ResponseBody
	@RequestMapping(value = "/createStoreQrCode", method = RequestMethod.POST)
	public AccessResponse<String> createStoreQrCode(@RequestParam("storeInfoId") String storeInfoId, HttpServletRequest request) {
		LogUtil.info("======根据门店ID生成门店二维码参数：storeInfoId=" + storeInfoId);

		try {
			if (StringUtils.isBlank(storeInfoId)) {
				return new AccessResponse<String>("-1", "门店ID不能为空");
			}

			// 根据门店主键获取门店信息
			StoreInfo storeInfo = storeService.selectByPrimaryKey(Integer.valueOf(storeInfoId));
			if (storeInfo == null || StringUtils.isBlank(storeInfo.getStoreCode())) {
				return new AccessResponse<String>("-1", "不存在该门店信息");
			}

			// 获取accessToken和mpId
			String accessToken = getMPAccessToken();
			int mpId = getMpId();

			// 生成门店二维码
			boolean createStoreQrcodeFlag = storeService.createStoreQrCode(accessToken, mpId, storeInfo,request.getSession().getServletContext().getRealPath("/"));
			if (createStoreQrcodeFlag) {
				return new AccessResponse<String>("0", "生成门店二维码成功");
			}
		} catch (NumberFormatException e) {
			LogUtil.error("====根据门店ID生成门店二维码异常", e);
		}
		return new AccessResponse<String>("-1", "生成门店二维码异常");
	}

}
